<?php
require_once 'albumDTO.php';
require_once 'dbHelper.php';

class albumDAO extends dbHelper
{
    public function create($albumDTO)
	{
		$sql = "insert into album(name, postDate, location, description, rate, accId, typeId)
                values('$albumDTO->name', '$albumDTO->postDate', '$albumDTO->location',
                        '$albumDTO->description', '$albumDTO->rate',
                        '$albumDTO->accId', '$albumDTO->typeId')";
		parent::execQuery($sql);
		if(parent::getAffectedRows() > 0)
			return true;
		return false;
	}
    
    public function update($albumDTO)
	{
		$sql = "update album set  name = '$albumDTO->name',
                                  location = '$albumDTO->location',
                                  description = '$albumDTO->description',
                                  typeId = '$albumDTO->typeId'";
		parent::execQuery($sql);
		if(parent::getAffectedRows() > 0)
			return true;
		return false;
	}
    
    public function delete($albId)
	{
		$sql = "delete from album where albId = '$albId'";
		parent::execQuery($sql);
		if(parent::getAffectedRows() > 0)
			return true;
		return false;
	}
    
    public function selecttop6_orderbypostDate()
	{
		$list = array();
		$sql = "select * from album order by postDate DESC limit 6";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$albDTO = new albumDTO($row[albId], $row[title],
                                       $row[postDate], $row[location],
                                       $row[image], $row[total],
                                       $row[description], $row[rate],
                                       $row[accId], $row[typeId]);
				$list[count($list)] = $albDTO;
			}
		}
		return $list;
	}
    
    public function selecttop10_orderbyrate()
	{
		$list = array();
		$sql = "select * from album order by rate DESC limit 10";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$albDTO = new albumDTO($row[albId], $row[title],
                                       $row[postDate], $row[location],
                                       $row[image], $row[total],
                                       $row[description], $row[rate],
                                       $row[accId], $row[typeId]);
				$list[count($list)] = $albDTO;
			}
		}
		return $list;
	}
    
    public function selecttop6_bytypeid_orderbypostDate($typeId)
	{
		$list = array();
		$sql = "select * from album where typeId = '$typeId' order by postDate DESC limit 6";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$albDTO = new albumDTO($row[albId], $row[title],
                                       $row[postDate], $row[location],
                                       $row[image], $row[total],
                                       $row[description], $row[rate],
                                       $row[accId], $row[typeId]);
				$list[count($list)] = $albDTO;
			}
		}
		return $list;
	}
    
    public function selecttop10_bytypeid_orderbyrate($typeId)
	{
		$list = array();
		$sql = "select * from album where typeId = '$typeId' order by rate DESC limit 10";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$albDTO = new albumDTO($row[albId], $row[title],
                                       $row[postDate], $row[location],
                                       $row[image], $row[total],
                                       $row[description], $row[rate],
                                       $row[accId], $row[typeId]);
				$list[count($list)] = $albDTO;
			}
		}
		return $list;
	}
    
    public function getListAlbum($conditions = array(), $offset = 0, $limit = 7)
    {
        $count = 'SELECT count(*) as total FROM album';
        $where = '';
        
        if($conditions['typeId'])
        {
            $where = ' `typeId` = ' . $conditions['typeId'];
        }
        
        if(!empty($where))
        {
            $count .= ' WHERE ' . $where;
        }
        
        $rsCount =  parent::execQuery($count);
        
        $total = 0;
        
        if($rsCount && parent::getNumRows($rsCount) > 0)
		{
		      $row = parent::fetchArray($rsCount);
              $total = $row['total'];
		}
        
        $list = array();
        
        if($total)
        {
            $query = 'SELECT * FROM album';
            
            if(!empty($where))
            {
               $query .= ' WHERE ' . $where;
            }
            
            $query .= ' LIMIT ' . $offset . ', ' .$limit;
            
            $resultQuery = parent::execQuery($query);
            
            while($row = parent::fetchArray($resultQuery))
			{
				$list[] = $row;
			}
        }
        
        return array('total' => $total, 'list' => $list);
    }
}
?>